package com.mxpio.erp.hr.controller;

import com.mxpio.erp.common.hr.entity.AttendanceCalendar;
import com.mxpio.erp.common.hr.service.AttendanceCalendarService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "AttendanceCalendarController", description = "考勤工作日历接口")
@RestController
@RequestMapping("/erp/hr/calendar/")
public class AttendanceCalendarController {
	
	@Autowired
	private AttendanceCalendarService service;

	@GetMapping("list")
	@Operation(summary = "考勤工作日历列表", description = "获取考勤工作日历列表", method = "GET")
	public Result<List<AttendanceCalendar>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<AttendanceCalendar> tos = service.list(AttendanceCalendar.class, criteria);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@Operation(summary = "考勤工作日历列表", description = "获取考勤工作日历列表", method = "GET")
	public Result<Page<AttendanceCalendar>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<AttendanceCalendar> page = service.listPage(AttendanceCalendar.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	
	@PostMapping("add")
	@Operation(summary = "新增考勤工作日历", description = "新增考勤工作日历", method = "POST")
	public Result<AttendanceCalendar> add(@RequestBody AttendanceCalendar calendar) {
		service.save(calendar);
		return Result.OK(calendar);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑考勤工作日历", description = "编辑考勤工作日历（全量）", method = "PUT")
	public Result<AttendanceCalendar> edit(@RequestBody AttendanceCalendar calendar) {
		service.update(calendar);
		return Result.OK(calendar);
	}
	

	@GetMapping("sync/{year}")
	@Operation(summary = "考勤工作日历接口同步", description = "考勤工作日历接口同步", method = "GET")
	public Result<?> sync(@PathVariable(name = "year", required = true) String year) {
		return service.sync(year);
	}
	
}
